package com.huijinhui.wlmtsys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huijinhui.wlmtsys.enums.AppHttpCodeEnum;
import com.huijinhui.wlmtsys.mapper.InfoMapper;
import com.huijinhui.wlmtsys.mapper.NewsMapper;
import com.huijinhui.wlmtsys.pojo.dtos.PageDTO;
import com.huijinhui.wlmtsys.pojo.dtos.ResponseResult;
import com.huijinhui.wlmtsys.pojo.entity.NewsPageQuery;
import com.huijinhui.wlmtsys.pojo.entity.PageQuery;
import com.huijinhui.wlmtsys.pojo.entity.WlmtsysInfo;
import com.huijinhui.wlmtsys.pojo.entity.WlmtsysNews;
import com.huijinhui.wlmtsys.service.FileStorageService;
import com.huijinhui.wlmtsys.service.InfoService;
import com.huijinhui.wlmtsys.service.NewsService;
import com.huijinhui.wlmtsys.utils.BeanUtils;
import com.huijinhui.wlmtsys.utils.CollUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NewsServiceImpl extends ServiceImpl<NewsMapper, WlmtsysNews> implements NewsService {

    @Autowired
    private  NewsMapper newsMapper;
    @Autowired
    private FileStorageService fileStorageService;



    /**
     * 获取新闻列表
     *
     * @return
     */
    @Override
    public List<WlmtsysNews> getNewsList(Integer lables ) {
        List<WlmtsysNews> newsList =this.lambdaQuery().eq(WlmtsysNews::getLabel,lables)
                .orderByDesc(WlmtsysNews::getDate).list();
        return newsList;
    }

    /**
     * 添加新闻
     *
     * @param news
     */
    @Override
    public void addNews(WlmtsysNews news) {
        if (news != null&&news.getDate() != null&&news.getTitle() != null&&news.getContent() != null) {
            this.save(news);
        }else{
            throw new IllegalArgumentException("news is null or date is null or title is null or content is null");
        }

    }

    /**
     * 删除新闻
     *
     * @param id
     */
    @Override
    public void deleteNews(Integer id) {
        if (id == null) {
            log.error("id is null");
            return;
        }
        log.info("deleteNews:{}", id);
        this.removeById(id);
    }

    /**
     * 更新新闻
     *
     * @param news
     */
    @Override
    public ResponseResult updateNews(WlmtsysNews news) {
        if (news == null) {
            log.error("news is null");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        log.info("updateNews:{}", news);
        this.updateById(news);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询新闻
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<WlmtsysNews> queryNewsPage(PageQuery query) {
        Page<WlmtsysNews> page = this.lambdaQuery()
                .eq(query.getLabel() != null, WlmtsysNews::getLabel,query.getLabel())
                .like(query.getContent() != null, WlmtsysNews::getContent, query.getContent())
                .between(query.getBeginTime() != null && query.getEndTime() != null,
                        WlmtsysNews::getDate, query.getBeginTime(), query.getEndTime())
                .orderByDesc(WlmtsysNews::getDate)
                .page(query.toMpPage("date", false));
        List<WlmtsysNews> records = page.getRecords();
        if (CollUtils.isEmpty(records)) {
            return PageDTO.empty(page);
        }
        List<WlmtsysNews> wlmtsysNews = new ArrayList<>();
        for (WlmtsysNews news : records) {
            WlmtsysNews wlmtsysNews1 = BeanUtils.copyBean(news, WlmtsysNews.class);
            wlmtsysNews.add(wlmtsysNews1);
        }
        return PageDTO.of(page, wlmtsysNews);
    }

    /**
     * 图片上传
     *
     * @param multipartFile
     */
    @Override
    public ResponseResult uploadNews(MultipartFile multipartFile, Integer newsId) {
        //1.检查参数
        if(multipartFile == null || multipartFile.getSize() == 0 || newsId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传图片到minIO中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //aa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        //3.更新数据库
        this.lambdaUpdate().eq(WlmtsysNews::getId,newsId).set(WlmtsysNews::getImage,fileId).update();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}




